package logic

import (
	"context"
	"fmt"
	"tiktok/models/mysql"

	"tiktok/tiktokrpc/internal/svc"
	"tiktok/tiktokrpc/tiktokrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录注册一体化
func (l *LoginLogic) Login(in *tiktokrpc.LoginRequest) (*tiktokrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	users := mysql.User{
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Where("account= ?", in.Account).Limit(1).Find(&users)
	if users.ID == 0 {
		mysql.DB.Create(&users)
		return nil, fmt.Errorf("用户不存在,已自动生成")
	}
	if users.Password != in.Password {
		return nil, fmt.Errorf("密码不正确")
	}
	return &tiktokrpc.LoginResponse{
		Id: int64(users.ID),
	}, nil
}
